﻿namespace UnityEditor
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    [Serializable]
    internal class FoldoutObjectState
    {
        private bool m_Animated;
        private int[] m_Children;
        private FoldoutComponentState[] m_Components;
        private bool m_Expanded;
        private GameObject m_Object;

        public FoldoutObjectState(GameObject obj, List<FoldoutObjectState> states, SerializedStringTable expandedFoldouts, int level)
        {
            this.m_Object = obj;
            states.Add(this);
            if (level == 0)
            {
                this.m_Expanded = true;
            }
            else if (expandedFoldouts.Contains(PathUtility.GetPath(obj.transform)))
            {
                this.m_Expanded = true;
            }
            List<int> list = new List<int>();
            IEnumerator enumerator = obj.transform.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    Transform current = (Transform) enumerator.Current;
                    list.Add(states.Count);
                    new FoldoutObjectState(current.gameObject, states, expandedFoldouts, level + 1);
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable == null)
                {
                }
                disposable.Dispose();
            }
            this.m_Children = list.ToArray();
        }

        public void AddChildCurvesToList(List<CurveState> curves, AnimationHierarchyData data)
        {
            if (this.expanded)
            {
                foreach (FoldoutComponentState state in this.components)
                {
                    state.AddChildCurvesToList(curves, data);
                }
                foreach (int num2 in this.children)
                {
                    data.states[num2].AddChildCurvesToList(curves, data);
                }
            }
        }

        public void Collapse(AnimationHierarchyData data, int level)
        {
            this.m_Expanded = false;
            this.Hide(data, level);
            if (level > 0)
            {
                data.expandedFoldouts.Remove(PathUtility.GetPath(this.obj.transform));
            }
            data.animationWindow.SetDirtyPropertyList();
        }

        public void Expand(AnimationHierarchyData data, int level)
        {
            this.m_Expanded = true;
            this.Show(data, level);
            if (level > 0)
            {
                data.expandedFoldouts.Set(PathUtility.GetPath(this.obj.transform));
            }
            data.animationWindow.SetDirtyPropertyList();
        }

        private void HandleClick(Rect r, AnimationHierarchyData data)
        {
            Event current = Event.current;
            if ((current.type == EventType.MouseDown) && r.Contains(current.mousePosition))
            {
                bool additive = current.shift || current.command;
                List<CurveState> curves = new List<CurveState>();
                this.AddChildCurvesToList(curves, data);
                if (curves.Count > 0)
                {
                    data.animationWindow.SelectProperties(curves[0], curves.Count, additive);
                }
            }
        }

        public void Hide(AnimationHierarchyData data, int level)
        {
            if (this.m_Components != null)
            {
                foreach (FoldoutComponentState state in this.m_Components)
                {
                    if (state.expanded)
                    {
                        state.Hide(data, this.obj.transform, level);
                    }
                }
            }
            foreach (int num2 in this.children)
            {
                if (data.states[num2].expanded)
                {
                    data.states[num2].Hide(data, level + 1);
                }
            }
        }

        public void OnGUI(AnimationHierarchyData data, string goPath, int level, List<CurveState> shownProperties)
        {
            GUILayoutOption[] options = new GUILayoutOption[] { GUILayout.ExpandWidth(true) };
            int width = (int) GUILayoutUtility.GetRect((float) 10f, (float) 0f, options).width;
            if (Event.current.type == EventType.Repaint)
            {
                data.animationWindow.DrawRowBackground(width, 15);
            }
            Rect objectLabelRect = data.animationWindow.GetObjectLabelRect((float) width);
            this.HandleClick(data.animationWindow.GetObjectLabelRect2((float) width), data);
            GUIContent content = new GUIContent(this.obj.name);
            bool flag = EditorGUI.Foldout(objectLabelRect, this.expanded, content);
            if (flag != this.expanded)
            {
                if (flag)
                {
                    this.Expand(data, level);
                }
                else
                {
                    this.Collapse(data, level);
                }
            }
            if (this.animated)
            {
                data.animationSelection.DrawRightIcon(CurveUtility.GetIconKey(), (Color) AnimationWindow.kAnimatedColor, (float) width);
            }
            AnimationWindow.vPosition += 15;
            if (this.expanded)
            {
                EditorGUI.indentLevel++;
                foreach (FoldoutComponentState state in this.components)
                {
                    if (state.animated || data.showAllProperties)
                    {
                        state.OnGUI(data, goPath, level, shownProperties, this.obj.transform);
                    }
                }
                foreach (int num3 in this.children)
                {
                    FoldoutObjectState state2 = data.states[num3];
                    if (state2.animated || data.showAllProperties)
                    {
                        string name;
                        if (goPath == string.Empty)
                        {
                            name = state2.obj.name;
                        }
                        else
                        {
                            name = goPath + "/" + state2.obj.name;
                        }
                        state2.OnGUI(data, name, level + 1, shownProperties);
                    }
                }
                EditorGUI.indentLevel--;
            }
        }

        public void RefreshAnimatedState(AnimationHierarchyData data)
        {
            this.m_Animated = false;
            string str = AnimationUtility.CalculateTransformPath(this.obj.transform, data.animated.transform);
            IEnumerator enumerator = data.animatedPaths.Keys.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    int current = (int) enumerator.Current;
                    if (current == str.GetHashCode())
                    {
                        this.m_Animated = true;
                    }
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable == null)
                {
                }
                disposable.Dispose();
            }
        }

        public void Show(AnimationHierarchyData data, int level)
        {
            this.RefreshAnimatedState(data);
            if (this.expanded)
            {
                UnityEngine.Object[] animatableObjects = AnimationUtility.GetAnimatableObjects(this.obj);
                if ((this.m_Components == null) || (this.m_Components.Length != animatableObjects.Length))
                {
                    this.m_Components = new FoldoutComponentState[animatableObjects.Length];
                    for (int j = 0; j < this.m_Components.Length; j++)
                    {
                        this.m_Components[j] = new FoldoutComponentState(animatableObjects[j], data.expandedFoldouts, this.obj.transform, level);
                    }
                }
                for (int i = 0; i < this.m_Components.Length; i++)
                {
                    this.m_Components[i].obj = animatableObjects[i];
                    this.m_Components[i].Show(data, this.obj.transform, level);
                }
                foreach (int num3 in this.children)
                {
                    data.states[num3].Show(data, level + 1);
                }
            }
        }

        public bool animated
        {
            get
            {
                return this.m_Animated;
            }
        }

        public int[] children
        {
            get
            {
                return this.m_Children;
            }
        }

        public FoldoutComponentState[] components
        {
            get
            {
                return this.m_Components;
            }
        }

        public bool expanded
        {
            get
            {
                return this.m_Expanded;
            }
        }

        public GameObject obj
        {
            get
            {
                return this.m_Object;
            }
        }
    }
}

